<?php

class Admin_UserController extends Zend_Controller_Action {

    public function init() {
        $this->view->headTitle('zendblog')->headTitle('admin')->setSeparator(' :: ');
        $this->view->title = 'admin';
    }

    public function listAction() {
        if (Zend_Auth::getInstance()->getIdentity()->admin) {
            $tableUsers = new Application_Model_DbTable_Users();
            $adapter = new Zend_Paginator_Adapter_DbSelect($tableUsers->select()
                                    ->from($tableUsers, array('username', 'disabled', 'id'))
                                    ->order('username desc')
            );
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(10);
            $paginator->setCurrentPageNumber($this->_getParam('page', 1));
            $this->view->paginator = $paginator;
        } else {
            $this->_forward('error', 'error', 'default');
        }
    }

    public function lockAction() {
        $this->_disableUser(true, $this->_getParam('id', 1));
        $this->_helper->getHelper('Redirector')->gotoSimple('list', 'user', 'admin',
                array('page' => $this->_getParam('page', 1)));
    }

    public function unlockAction() {
        $this->_disableUser(false, $this->_getParam('id', 1));
        $this->_helper->getHelper('Redirector')->gotoSimple('list', 'user', 'admin',
                array('page' => $this->_getParam('page', 1)));
    }

    protected function _disableUser($lock, $id) {
        $tableUser = new Application_Model_DbTable_Users();
        $data = array(
            'disabled' => $lock,
        );
        $tableUser->update($data,
                $tableUser->getAdapter()->quoteInto('id = ?', $id)
        );
    }

}

